<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;


class Project extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'project';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'status_text',
        'user_id_text',
        'customer_company',
        'customer_name',
    ];


    protected static function init() {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getStatusList() {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getUserIdTextAttr($value, $data){
        $str = Admin::where('id',$data['user_id'])->value('nickname');
        return $str?$str:'-';
    }

    public function admin() {
        return $this->hasOne('admin', 'id', 'user_id');
    }


    public function getStarttimeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCustomerCompanyAttr($value, $data) {
        $str = Customer::where(['id'=>$data['customer_id']])->value('company');
        return $str ? $str : '-';
    }

    public function getCustomerNameAttr($value, $data) {
        $str = Customer::where(['id'=>$data['customer_id']])->value('name');
        return $str ? $str : '-';
    }

    protected function setStarttimeAttr($value) {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value) {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    //修改责任人
    protected function setAdminIdAttr($value) {
        if (!is_numeric($value)) {
            return Admin::where('nickname', $value)->value('id') ? Admin::where('nickname', $value)->value('id') : 0;
        }
        return $value;
    }


    //修改项目
    protected function setProjectIdAttr($value) {
        if (!is_numeric($value)) {
            return Project::where('title', $value)->value('id') ? Project::where('title', $value)->value('id') : 0;
        }
        return $value;
    }


    //获取信息
    public function getInfo($id=null,$user_id=null,$admin_id=null,$status=null,$order="weigh desc,id desc",$page=1,$pageCount=10) {
        $offset = ($page-1)*$pageCount;
        $where = [];
        if($id){$where['id'] = ['=',$id];}
        if($user_id){$where['user_id'] = ['=',$user_id];}
        if($admin_id){$where[] = ['exp',Db::raw("FIND_IN_SET($admin_id,admin_ids)")];}
        if($status){$where['status'] = ['=',$status];}
        $info = collection(self::where($where)->order($order)->limit($offset,$pageCount)->select())->toArray();
        return $info;
    }

}
